package com.vehicle.admin.web.controller;

import com.vehicle.admin.web.common.controller.BaseController;
import com.vehicle.admin.web.common.shiro.token.TokenManager;
import com.vehicle.common.util.encryption.MD5Utils;
import com.vehicle.common.web.JsonResult;
import com.vehicle.ucenter.api.entity.SysMenu;
import com.vehicle.ucenter.api.entity.SysUser;
import com.vehicle.ucenter.api.service.SysMenuService;
import com.vehicle.ucenter.api.service.SysUserService;
import org.apache.shiro.authc.AccountException;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.DisabledAccountException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.web.filter.authc.FormAuthenticationFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;
import java.util.Map;
import java.util.Objects;


/**
 * Created by yantingbin on 2017/12/11.
 */
@Controller
@RequestMapping(value = "/home")
public class IndexController extends BaseController {

    private static final Logger LOGGER = LoggerFactory.getLogger(IndexController.class);

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysMenuService sysMenuService;

/*    @Autowired
    private JavaMailSender mailSender;*/

    @GetMapping(value = "/login")
    public String login() {
        return "home/login";
    }

    @GetMapping(value = "index_1")
    public String showMaster() {
        return "/home/md";
    }

    @GetMapping(value = "/index")
    public String index(Model model) {
        List<SysMenu> menus = sysMenuService.getMenuNav(TokenManager.getToken().getId());
        model.addAttribute("menus", menus);
        return "home/index";
    }

    @PostMapping(value = "/submitLogin")
    public String submitLogin(String username, String password, Map<String, Object> map) {
        try {
            TokenManager.login(username, MD5Utils.encryptMD5(password));

            return "redirect:/home/index";
        } catch (DisabledAccountException dae) {
            map.put("msg", "账号已被禁用！");
            return "/home/login";
        } catch (AccountException | IncorrectCredentialsException ice) {
            LOGGER.error("login exception", ice);

            map.put("msg", "账号或密码不正确！！");
            return "/home/login";
        }
    }

    @GetMapping(value = "/register")
    public String register() {
        return "/home/register";
    }

    @PostMapping(value = "sendCode")
    public JsonResult sendCode(String mail) {
        mail = mail.replace("%40", "@");

        SysUser user = sysUserService.getByUserEmail(mail);

        if(Objects.isNull(user)){
            return renderError("邮箱已注册");
        }

        return null;
    }

}
